/*	Author		: Arga
 *	Created On	: Apr 28, 2011
 *	Description	: 
 */

package team8.paacdroid.activity;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import team8.paacdroid.R;
import team8.paacdroid.bean.BusRide;
import team8.paacdroid.bean.Itinerary;
import team8.paacdroid.bean.Schedule;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

public class ViewItineraryDetailsActivity extends RootActivity {
	// UI elements declarations
	TextView tvPrivacyPolicy;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Load the layout
		setContentView(R.layout.view_itinerary_details_activity);
	}
	
	private ArrayList<Itinerary> createFallBackPlan() throws ParseException {
		ArrayList<Itinerary> fallBackPlan = new ArrayList<Itinerary>();
		
		Itinerary itin2 = new Itinerary();
		itin2.setDestinationText("S CRAIG ST");
		itin2.setNumOfBusRides(2);
		itin2.setOriginText("BAUM BLVD & S NEGLEY AVE");
		itin2.setTripDate("5/2/2011");
		itin2.setTripName("BAUM BLVD & S NEGLEY AVE to S CRAIG ST");
		itin2.setTripTime(21);
		ArrayList<BusRide> buses2 = new ArrayList<BusRide>();
		BusRide bus2A = new BusRide();
		bus2A.setBoardInstruction("Board at");
		bus2A.setBoardStopId(6824);
		bus2A.setBoardStopInfoLink("http://www.portauthority.org/atisdnn/pgStopDetails.aspx?ATISStopID=6824&stop=NEGLEY AVE and #370 (BAUM BLVD)&Lat=40.458663&Long=-79.933726&Route=71A&Scheduledate=&Direction=I");
		bus2A.setBoardStopLat(40.458663);
		bus2A.setBoardStopLong(-79.933726);
		bus2A.setBoardStopName("NEGLEY AVE & #370 (BAUM BLVD)");
		bus2A.setBoardTime("5/2/2011 11:04 PM");
		bus2A.setDetour(false);
		bus2A.setDirection("Inbound");
		bus2A.setOffInstruction("Get off at");
		bus2A.setOffStopId(4827);
		bus2A.setOffStopInfoLink("http://www.portauthority.org/atisdnn/pgStopDetails.aspx?ATISStopID=4827&stop=CENTRE AVE and CRAIG&Lat=40.451969&Long=-79.951777&Route=71A&Scheduledate=&Direction=I");
		bus2A.setOffStopLat(40.451969);
		bus2A.setOffStopLong(-79.951777);
		bus2A.setOffStopName("CENTRE AVE & CRAIG");
		bus2A.setOffTime("5/2/2011 11:09 PM");
		bus2A.setPdfScheduleLink("http://www.portauthority.org/paac/apps/pdfs/71A.pdf");
		bus2A.setQuickScheduleLink("http://www.portauthority.org/atisdnn/TimeTable.aspx?Route=71A&Scheduledate=&Direction=I");
		bus2A.setRouteName("NEGLEY DOWNTOWN VIA OAKLAND");
		bus2A.setRouteNo("71A");
		buses2.add(bus2A);
		BusRide bus2B = new BusRide();
		bus2B.setBoardInstruction("Board at");
		bus2B.setBoardStopId(4827);
		bus2B.setBoardStopInfoLink("http://www.portauthority.org/atisdnn/pgStopDetails.aspx?ATISStopID=4827&stop=CENTRE AVE and CRAIG&Lat=40.451969&Long=-79.951777&Route=54&Scheduledate=&Direction=O");
		bus2B.setBoardStopLat(40.451969);
		bus2B.setBoardStopLong(-79.951777);
		bus2B.setBoardStopName("CENTRE AVE & CRAIG");
		bus2B.setBoardTime("5/2/2011 11:23 PM");
		bus2B.setDetour(false);
		bus2B.setDirection("Outbound");
		bus2B.setOffInstruction("Get off at");
		bus2B.setOffStopId(4983);
		bus2B.setOffStopInfoLink("http://www.portauthority.org/atisdnn/pgStopDetails.aspx?ATISStopID=4983&stop=CRAIG ST and PGH BLIND ASSOCIATION&Lat=40.445866&Long=-79.948930&Route=54&Scheduledate=&Direction=O");
		bus2B.setOffStopLat(40.445866);
		bus2B.setOffStopLong(-79.948930);
		bus2B.setOffStopName("CRAIG ST & PGH BLIND ASSOCIATION");
		bus2B.setOffTime("5/2/2011 11:25 PM");
		bus2B.setPdfScheduleLink("http://www.portauthority.org/paac/apps/pdfs/54.pdf");
		bus2B.setQuickScheduleLink("http://www.portauthority.org/atisdnn/TimeTable.aspx?Route=54&Scheduledate=&Direction=O");
		bus2B.setRouteName("OAKLAND-SOUTH SIDE SOUTH HILLS JCT VIA PENN & MAIN");
		bus2B.setRouteNo("54");
		buses2.add(bus2B);
		itin2.setBusRides(buses2);
		fallBackPlan.add(itin2);
		
		return fallBackPlan;
	}

	public void showMap1(View view) {
		doShowMap(1);
	}
	
	public void showQuickSchedule1(View view) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		ScheduleFinderActivity sfa = new ScheduleFinderActivity();
		result.put("Type", "Schedule");
		result.put("Schedule", sfa.grabSchedule(""));
	
		// Create new intent
		Intent intentViewSchedule = new Intent(this, ViewScheduleActivity.class);
		
		// Create and attach the data bundle
		Bundle bundleSchedule = new Bundle();
		if (result.get("Schedule") != null && result.get("Schedule").getClass().equals(Schedule.class)) {
			bundleSchedule.putSerializable("Schedule", (Schedule) result.get("Schedule"));
			intentViewSchedule.putExtras(bundleSchedule);
		}
		
		// Directly call the activity
		startActivity(intentViewSchedule);
	}
	
	public void showPdfSchedule1(View view) {
		doShowQuickSchedule();
	}

	public void showMap2(View view) {
		doShowMap(2);
	}
	
	public void showQuickSchedule2(View view) {
		doShowQuickSchedule();
	}
	
	public void showPdfSchedule2(View view) {
		doShowPdfSchedule(2);
	}
	
	public void doShowPdfSchedule(int busRideNo) {
		Uri uri = null;
		try {
			uri = Uri.parse(createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getPdfScheduleLink());
		} catch (ParseException e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("An error had occured: ParseException.");
			AlertDialog alert = builder.create();
			alert.show();
			Log.e(getString(R.string.app_name), "ParseException: " + e.getMessage());
			e.printStackTrace();
		}
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}
	
	public void doShowQuickSchedule() {
		HashMap<String, Object> result = new HashMap<String, Object>();
		ScheduleFinderActivity sfa = new ScheduleFinderActivity();
		result.put("Type", "Schedule");
		result.put("Schedule", sfa.grabSchedule(""));
	
		// Create new intent
		Intent intentViewSchedule = new Intent(this, ViewScheduleActivity.class);
		
		// Create and attach the data bundle
		Bundle bundleSchedule = new Bundle();
		if (result.get("Schedule") != null && result.get("Schedule").getClass().equals(Schedule.class)) {
			bundleSchedule.putSerializable("Schedule", (Schedule) result.get("Schedule"));
			intentViewSchedule.putExtras(bundleSchedule);
		}
		
		// Directly call the activity
		startActivity(intentViewSchedule);
	}
	
	public void doShowMap(int busRideNo) {
		// Prepare geo location hashmap
		HashMap<String, String> geoLocPackage = new HashMap<String, String>();
		try {
			geoLocPackage.put("fromLat", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getBoardStopLat().toString());
			geoLocPackage.put("fromLong", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getBoardStopLong().toString());
			geoLocPackage.put("toLat", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getOffStopLat().toString());
			geoLocPackage.put("toLong", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getOffStopLong().toString());
			geoLocPackage.put("fromStopName", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getBoardStopName());
			geoLocPackage.put("toStopName", createFallBackPlan().get(0).getBusRides().get(busRideNo-1).getOffStopName());
		} catch (ParseException e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("An error had occured: ParseException.");
			AlertDialog alert = builder.create();
			alert.show();
			Log.e(getString(R.string.app_name), "ParseException: " + e.getMessage());
			e.printStackTrace();
		}
		
		// Create new intent
		Intent intentViewMap = new Intent(this, ViewMapActivity.class);
		
		// Create and attach the data bundle
		Bundle bundleRoute = new Bundle();
		bundleRoute.putSerializable("Route", geoLocPackage);
		intentViewMap.putExtras(bundleRoute);
		
		// Directly call the activity
		startActivity(intentViewMap);
	}
}
